using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Orders : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["ShoppingCart"] == null)
        {
            Literal1.Text = "Sorry, Your shopping cart is empty";
            this.btnOrder.Enabled = false;                 
        }
        else
        {                 
            this.btnOrder.Enabled = true;                 
            DataTable dataTable = (DataTable)Session["ShoppingCart"];
            GetTotalAmount(dataTable, this.Literal2);
            DataList1.DataSource = dataTable;
            DataList1.DataBind();
        }
        
    }
    void GetTotalAmount(DataTable dataTable, Literal literal)
    {
        int totalAmount = 0;
        foreach(DataRow dataRow in dataTable.Rows)
        {
            totalAmount += Convert.ToInt32(dataRow["Quantity"]) 
            *  Convert.ToInt32(dataRow["Price"]);
        }
        literal.Text = "Total Amount: " + totalAmount.ToString("#,###,00");
    }
    protected void btnOrder_Click(object sender, EventArgs e)
    {
        try
        {
            string connectionString =
            WebConfigurationManager.ConnectionStrings[
            "InterShop"].ConnectionString;
            SqlServerProvider sqlServerProvider = 
            new SqlServerProvider(connectionString);
            
            sqlServerProvider.CommandText =
            "SPOrders";
            sqlServerProvider.CommandType = 
            CommandType.StoredProcedure ;
            
            sqlServerProvider.ParameterCollection 
            = new string[7] {"@CustomerID", 
            "@CustomerName", 
            "@ContactPerson","@Address", "@Tel",
            "@PaymentType", "@Description"};
            
            sqlServerProvider.ValueCollection
            = new string[7] {"0", this.txtFullName.Text, 
            this.txtContact.Text, txtAddress.Text, 
            txtTel.Text, ddlPayType.SelectedValue,
            txtDescription.Text};          
            sqlServerProvider.ReturnValueParameter = 0;
            sqlServerProvider.ExecuteNonQuery();
            int returnValue = sqlServerProvider.ReturnValue;
            
            sqlServerProvider.CommandText =
            "SPOrderDetails";
            sqlServerProvider.ParameterCollection
            = new string[4] {"@CustomerID", 
            "@ItemID","@Quantity", "@Price"};
            
            sqlServerProvider.ValueCollection
            = new string[4] {returnValue.ToString(),
             "", "", "" };
            sqlServerProvider.ReturnValueParameter = -1; 
            for(int i=0; i<DataList1.Items.Count; i++)
            {
                Label labelID = 
                (Label)DataList1.Items[i].FindControl("lblID");
                sqlServerProvider.ValueCollection[1] = labelID.Text;
                labelID =
                (Label)DataList1.Items[i].FindControl("lblQuantity");
                sqlServerProvider.ValueCollection[2] = labelID.Text;
                labelID =
                (Label)DataList1.Items[i].FindControl("lblPrice");
                sqlServerProvider.ValueCollection[3] = labelID.Text;
                sqlServerProvider.ExecuteNonQuery();
            }
           
            Session.Remove("ShoppingCart");
            Response.Redirect("Thankyou.aspx"); ;
        }
        catch(Exception ex)
        {
            Literal1.Text = ex.Message;
            
        }
    }
}
